package com.lwlk.zdk.controller.Platform;

import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.T.TDisEnterpriseMapper;
import com.lwlk.zdk.model.Excel.ErrorMessage;
import com.lwlk.zdk.model.T.TDisEnterprise;
import com.lwlk.zdk.model.T.TDisPlatform;
import com.lwlk.zdk.model.sys.sysUser;
import com.lwlk.zdk.service.User.SysUserService;
import com.lwlk.zdk.util.POIUtil;
import com.lwlk.zdk.util.Response;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;


@RestController
@RequestMapping(value = "/excel")
public class ExcelUserController {


    private static Logger logger  = Logger.getLogger(ExcelUserController.class);

    @Autowired
    TDisEnterpriseMapper disEnterpriseMapper;

    //==========导入==========
    @RequestMapping(value = "/IntoExcel",method = RequestMethod.POST)
    public Response IntoExcel(MultipartFile file) throws Exception {

        Response response=new Response();
        Workbook workbook= POIUtil.getWorkBook(file);
        System.out.println(workbook);
        String fileName = file.getOriginalFilename();
        List<ErrorMessage> errorMessages =null;
        ErrorMessage errorMessage = null;
        if(fileName.endsWith("xls")){
           // workbook = new HSSFWorkbook(is);
            Sheet shto = workbook.getSheetAt(0);
            for (Row row : shto) {
                if (row.getRowNum() < 1) {
                    continue;
                }
                    //创建实体类
                     TDisEnterprise disEnterprise=new TDisEnterprise();

                     disEnterprise.setEnterpriseName(row.getCell(0).getStringCellValue());//业户名称
                //获取单元格
                XSSFCell cell= (XSSFCell) row.getCell(2);
                XSSFCell cell1= (XSSFCell) row.getCell(4);
                XSSFCell cell2= (XSSFCell) row.getCell(6);
                XSSFCell cell3= (XSSFCell) row.getCell(8);
                //设置单元格类型
                cell.setCellType(CellType.STRING);
                //获取单元格数据
                Integer RegionCode = Integer.valueOf(cell.getStringCellValue());
                Integer IndustryId = Integer.valueOf(cell1.getStringCellValue());
                Integer OrganizationId = Integer.valueOf(cell2.getStringCellValue());
                Integer ManagementForms = Integer.valueOf(cell3.getStringCellValue());
                disEnterprise.setRegionCode(RegionCode);//所属地区
                disEnterprise.setIndustryId(IndustryId);//所属行业
                disEnterprise.setOrganizationId(OrganizationId);//管辖机构
                disEnterprise.setManagementForms(ManagementForms);//经营状态
                    disEnterprise.setScope(String.valueOf(Integer.valueOf(row.getCell(10).getStringCellValue())));//经营范围
                    disEnterpriseMapper.insertSelective(disEnterprise);
            }
        }
        if(fileName.endsWith("xlsx")){
          //  workbook = new XSSFWorkbook(is);
            Sheet shto = workbook.getSheetAt(0);
            for (Row row : shto) {
                if (row.getRowNum() < 1) {
                    continue;
                }
                //创建实体类
                //创建实体类
                TDisEnterprise disEnterprise=new TDisEnterprise();
                disEnterprise.setEnterpriseName(row.getCell(0).getStringCellValue());//业户名称
                //获取单元格
                XSSFCell cell= (XSSFCell) row.getCell(2);
                XSSFCell cell1= (XSSFCell) row.getCell(4);
                XSSFCell cell2= (XSSFCell) row.getCell(6);
                XSSFCell cell3= (XSSFCell) row.getCell(8);
                //设置单元格类型
                cell.setCellType(CellType.STRING);
                //获取单元格数据
                Integer RegionCode = Integer.valueOf(cell.getStringCellValue());
                Integer IndustryId = Integer.valueOf(cell1.getStringCellValue());
                Integer OrganizationId = Integer.valueOf(cell2.getStringCellValue());
                Integer ManagementForms = Integer.valueOf(cell3.getStringCellValue());
                disEnterprise.setRegionCode(RegionCode);//所属地区
                disEnterprise.setIndustryId(IndustryId);//所属行业
                disEnterprise.setOrganizationId(OrganizationId);//管辖机构
                disEnterprise.setManagementForms(ManagementForms);//经营状态
                disEnterprise.setScope(String.valueOf(Integer.valueOf(row.getCell(10).getStringCellValue())));//经营范围
                disEnterpriseMapper.insertSelective(disEnterprise);
          /*      if (row.getCell(4) != null) {
                    row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                }*/
            }
        }
        response.setData(1);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;

    }


    //导出
    @RequestMapping(value = "/OutExcel",method = RequestMethod.GET)
    public Response OutExcel(HttpServletResponse response,String platformId){
        Response resp=new Response();
        try {
            String[] platformIds=platformId.split(",");
            for (String id : platformIds) {

            }
            //创建HSSFWorkbook对象(excel的文档对象)
            XSSFWorkbook wb=new XSSFWorkbook();
            //创建新的sheet对象(excel的表单)
            XSSFSheet sheet=wb.createSheet("用户表");
            //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
            XSSFRow row=sheet.createRow(0);
            //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
            XSSFCell cell=row.createCell(0);
            //设置单元格内容
            cell.setCellValue("用户信息展示表");
            //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
            //在sheet里创建第二行
            XSSFRow row2=sheet.createRow(1);
            //创建单元格并设置单元格内容
            row2.createCell(0).setCellValue("登录名");
            row2.createCell(2).setCellValue("账号");
            row2.createCell(4).setCellValue("密码");
            //在sheet里创建第三行
            XSSFRow row3=sheet.createRow(2);
            row3.createCell(0).setCellValue("测试2");
            row3.createCell(2).setCellValue("root");
            row3.createCell(4).setCellValue("root");
            //输出Excel文件
            OutputStream output=response.getOutputStream();
            response.setHeader("Content-disposition", "attachment; filename=用户表.xls");
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        resp.setData(1);
        resp.setCode(RetCode.SUCCESS);
        resp.setMsg("执行成功");
        return resp;
    }




}
